import { ENUM_ERROR, ENUM_USER, RestDto, User } from "qqlx-core";

/** 访客必须是主账号，并且传入的 masterId 必须和访客一致（否则会报错）*/
export function doMasterCheck(rest: RestDto, someoneId: User["uuid32"]) {
  const { visitorId, visitorType } = rest;
  const isSame = visitorId === someoneId;
  const isPrimary = visitorType === ENUM_USER.PRIMARY;

  const isGrant = isSame && isPrimary;
  if (!isGrant) throw { code: ENUM_ERROR.DROIT_BELOW };
  return { masterId: someoneId };
}

export function getUserName(user?: User, defauleName: string = "默认昵称") {
  return user?.nickname || user?.justUserWeChat?.nickname || user?.justUserPassword?.accountNumber || user?.justUserEmail?.email || defauleName;
}

export function getUserAvatar(user?: User, defaultURL: string = "https://pic4.zhimg.com/50/v2-6afa72220d29f045c15217aa6b275808_hd.jpg") {
  return user?.avatar || user?.justUserWeChat?.avatar || defaultURL;
}

export function getDefaultRestDto(): RestDto {
  return {
    traceInfo: { traceId: "", traceIp: "", action: "", path: "" },
    visitorId: "",
    visitorType: ENUM_USER.NONE,
  };
}
